package mededelingen.view;

import java.awt.*;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpinnerDateModel;

import mededelingen.controller.MededelingController;
import mededelingen.model.MededelingenListModel;
import mededelingen.model.Mededelingenlijst;
import account.model.HelpListModel;
import account.student.controller.StudentController;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JComboBox;

import org.jdesktop.swingx.JXDatePicker;

import tools.FieldLimit;

import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;


public class MededelingenView extends JFrame {

	private static final long serialVersionUID = 1L;
	private static MededelingenView instance;
	
	private JTable table = new JTable(new MededelingenListModel());
	private MededelingController mc = new MededelingController(table, null);
	
	private JMenuBar menuBar = new JMenuBar();
	private JMenu mnStart = new JMenu("Start");
	private JMenuItem mntmStop = new JMenuItem("Afmelden");
	private JMenuItem menuItem_2 = new JMenuItem("Account beheren");
	private JMenu mnHelp = new JMenu("Mededeling");
	private JMenuItem mntmUpdates = new JMenuItem("Verwijderen");
	private JMenuItem mntmHelp = new JMenuItem("Toevoegen");
	
	private JTextField txtNaam = new JTextField();
	private JTextArea txtBeschrijving = new JTextArea();
	private JSpinner txtBegindatum = new JSpinner(new SpinnerDateModel());
	private JSpinner txtEinddatum = new JSpinner(new SpinnerDateModel());

	public static MededelingenView getInstance(){
		if(instance == null){			
			instance = new MededelingenView();
		}
		return instance;
	}

	private MededelingenView() {
		GUI();
		Mededelingenlijst.getInstance().loadMededeling();
	}

	private void GUI() {
		setJMenuBar(menuBar);
		menuBar.add(mnStart);
		mntmStop.setActionCommand("Afmelden");
		mntmStop.addActionListener(new StudentController(mntmStop));
		mnStart.add(mntmStop);
		menuBar.add(mnHelp);
		mntmHelp.setActionCommand("Help");
		mnHelp.add(mntmHelp);
		mntmUpdates.setActionCommand("Help");
		mnHelp.add(mntmUpdates);
		
		txtNaam.setColumns(10);
		txtNaam.setDocument(new FieldLimit(40));
		txtBeschrijving.setLineWrap(true);
		txtBeschrijving.setWrapStyleWord(true);
		txtBeschrijving.setDocument(new FieldLimit(140));
		txtBeschrijving.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		//nederlandse datum
		JSpinner.DateEditor dateEditor1 = new JSpinner.DateEditor(txtBegindatum, "dd-MM-yyyy");
		JSpinner.DateEditor dateEditor2 = new JSpinner.DateEditor(txtEinddatum, "dd-MM-yyyy");
		txtBegindatum.setEditor(dateEditor1);
		txtEinddatum.setEditor(dateEditor2);	

		JPanel panel = new JPanel();
		JLabel lblBegindatum = new JLabel("Begindatum");
		JLabel lblEinddatum = new JLabel("Einddatum");
		JLabel lblNaam = new JLabel("Titel");
		JLabel lblBeschrijving = new JLabel("Beschrijving");

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setEnabled(false);
		scrollPane.setViewportView(table);
		
		//button bijwerken
		JButton btnBijwerken = new JButton("Bijwerken");
		btnBijwerken.addActionListener(new MededelingController(table, btnBijwerken));
		btnBijwerken.setActionCommand("Bijwerken");

		//button toevoegen
		JButton btnToevoegen = new JButton("Toevoegen");
		btnToevoegen.addActionListener(new MededelingController(table, btnToevoegen));
		btnToevoegen.setActionCommand("Toevoegen");

		//button verwijderen
		JButton btnVerwijderen = new JButton("Verwijderen");
		btnVerwijderen.addActionListener(new MededelingController(table, btnVerwijderen));
		btnVerwijderen.setActionCommand("Verwijderen");
		
		//table mouseclick
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				mc.tableKlik();}
		});
		
		//hide id column
		table.getColumnModel().getColumn(0).setMinWidth(0);
		table.getColumnModel().getColumn(0).setMaxWidth(0);
		table.getColumnModel().getColumn(0).setPreferredWidth(0);
		
		//column settings
		table.getColumnModel().getColumn(1).setMinWidth(90);
		table.getColumnModel().getColumn(2).setMinWidth(90);
		table.getColumnModel().getColumn(3).setMinWidth(50);
		table.getColumnModel().getColumn(4).setMinWidth(150);
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
							.addGroup(gl_panel.createSequentialGroup()
								.addComponent(lblBegindatum, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(txtBegindatum, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE))
							.addGroup(gl_panel.createSequentialGroup()
								.addComponent(lblEinddatum)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(txtEinddatum, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
							.addGroup(gl_panel.createSequentialGroup()
								.addComponent(btnToevoegen, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnBijwerken, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnVerwijderen, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
							.addGroup(gl_panel.createSequentialGroup()
								.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
									.addComponent(lblBeschrijving)
									.addComponent(lblNaam))
								.addGap(20)
								.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(txtBeschrijving)
									.addComponent(txtNaam, GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)))))
					.addContainerGap(162, Short.MAX_VALUE))
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtNaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNaam))
					.addGap(12)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtBeschrijving, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblBeschrijving))
					.addGap(24)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBegindatum)
						.addComponent(txtBegindatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblEinddatum)
						.addComponent(txtEinddatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnToevoegen)
						.addComponent(btnBijwerken)
						.addComponent(btnVerwijderen))
					.addContainerGap(73, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 489, Short.MAX_VALUE)
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(11)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 482, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		
		//frame settings
		setTitle("Mededelingen bewerken");
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		getContentPane().setEnabled(false);
		pack();
		
		//center
		Dimension center = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(center.width / 2 - this.getSize().width / 2,
				center.height / 2 - this.getSize().height / 2);
	}

	public JTextField getTxtNaam() {
		return txtNaam;
	}

	public void setTxtNaam(JTextField txtNaam) {
		this.txtNaam = txtNaam;
	}

	public JTextArea getTxtBeschrijving() {
		return txtBeschrijving;
	}

	public void setTxtBeschrijving(JTextArea txtBeschrijving) {
		this.txtBeschrijving = txtBeschrijving;
	}

	public JSpinner getTxtBegindatum() {
		return txtBegindatum;
	}

	public void setTxtBegindatum(JSpinner txtBegindatum) {
		this.txtBegindatum = txtBegindatum;
	}

	public JSpinner getTxtEinddatum() {
		return txtEinddatum;
	}

	public void setTxtEinddatum(JSpinner txtEinddatum) {
		this.txtEinddatum = txtEinddatum;
	}
}
